package com.luojun.servlets;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.luojun.entity.Commodity;
import com.luojun.entity.User;
import com.luojun.service.UCommodityService;

/**
 * Servlet implementation class DoUpdateCommodity
 */
@WebServlet("/DoUpdateCommodity")
public class DoUpdateCommodity extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoUpdateCommodity() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		int commodityid = 0;
		String name= "";
		String describe = "";
		String price = "";
		String picture = "";
		String fileName ="";
			// 1 创建管理磁盘的工厂
			DiskFileItemFactory factory = new DiskFileItemFactory();
			// 2创建处理文件和解析文件的类
			ServletFileUpload upload = new ServletFileUpload(factory);
			try {
				// 3解析表单里面的每一个元素
				List<FileItem> list = upload.parseRequest(request);
			
				for (FileItem fileItem : list) {
					
					if (fileItem.isFormField()) { // 判断是普通元素，还是文件元素
						if (fileItem.getFieldName().equals("name")) {
							name = fileItem.getString("utf-8");// 防止中文乱码
						} else if (fileItem.getFieldName().equals("describe")) {
							describe = fileItem.getString("utf-8");
						} else if(fileItem.getFieldName().equals("price")){
							price = fileItem.getString();
						} else if(fileItem.getFieldName().equals("id")){
							commodityid = Integer.parseInt(fileItem.getString());
						} else if(fileItem.getFieldName().equals("pic")){
							picture = fileItem.getString();						
						}
					} else {
						
						fileName = fileItem.getName();
						long size = fileItem.getSize();
						if (size == 0 || fileName == null) {
							fileName=picture;
							break;
						}					
						picture = request.getServletContext().getRealPath("/")+
							"static/img/" + fileName;
						
						
						File file = new File(picture);
						fileItem.write(file);
						fileName="/static/img/"+fileName;
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//---------------------------
			
			//-----------------------------------
			User user = (User)session.getAttribute("user");	
			Commodity commodity = new Commodity();
			commodity.setCommodityId(commodityid);
			commodity.setName(name);
			commodity.setDescribe(describe);
			commodity.setPicture(fileName);
			commodity.setPrice(Integer.parseInt(price));
			commodity.setUser(user);
			System.out.println(commodity);
			UCommodityService ucs = new UCommodityService();
			ucs.update(commodity);
			response.sendRedirect("ToPersonalCenter");
	}

}
